package samber

import (
	"fmt"
	"github.com/samber/lo"
)

// Partial 封装一个函数带一个可变的固定值
func Partial() {
	arg1 := 999
	resFunc := lo.Partial(func(a int, b string) int64 {
		fmt.Println(a, b) // 999 6
		return int64(1)
	}, arg1)
	res := resFunc("6")
	fmt.Printf("%v \n", res)
}

// PartitionBy 按照顺序划分为组
func PartitionBy() {
	list := []int{-2, -1, 0, 1, 2, 3, 4}
	result := lo.PartitionBy(list, func(x int) string {
		if x < 0 {
			return "negative"
		} else if x%2 == 0 {
			return "even"
		}
		return "odd"
	})
	fmt.Printf("%v \n", result) // [[-2 -1] [0 2 4] [1 3]]
}

func PickBy() {
	kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
	result := lo.PickBy(kv, func(key string, value int) bool {
		return value%2 == 1
	})
	fmt.Printf("%v \n", result) // map[baz:3 foo:1]
}

func PickByKeys() {
	kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
	result := lo.PickByKeys(kv, []string{"foo", "baz"})
	fmt.Printf("%v \n", result) // map[baz:3 foo:1]
}

func PickByValues() {
	kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
	result := lo.PickByValues(kv, []int{1, 3})
	fmt.Printf("%v \n", result) // map[baz:3 foo:1]
}
